package com.czk.ssaaliyunworkflow.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-11-05
 * @Description:
 * @Version: 1.0
 */
@RestController
public class AliyunWorkFlowController {

    @Resource(name = "qwenChatClient")
    private ChatClient chatClient;
    @Resource
    private DashScopeApi dashScopeApi;
    /**
     * 工作流id
     * 百炼平台的创建的
     */
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String appId;

    private DashScopeAgent dashScopeAgent;

    /**
     * 构造器注入
     *
     * @param dashScopeAgentApi
     */
    public AliyunWorkFlowController(DashScopeAgentApi dashScopeAgentApi) {
        this.dashScopeAgent = new DashScopeAgent(dashScopeAgentApi);

    }

    /**
     * 调用阿里云上的知识库构建工作流服务
     *
     * @param msg
     * @return
     */
    @GetMapping("/aliyun-workflow/chat")
    public Flux<String> chat(@RequestParam(name = "msg", defaultValue = "今天吃什么") String msg) {

        DashScopeAgentOptions dashScopeAgentOptions = DashScopeAgentOptions.builder()
                .withAppId(appId)
                .build();
        Prompt prompt = new Prompt(msg, dashScopeAgentOptions);
        return dashScopeAgent.stream(prompt)
                .mapNotNull(response -> response.getResult().getOutput().getText());
    }


}
